#ifndef QMISSION_H
#define QMISSION_H

#include "communicate_global.h"

#include <QObject>
#include <QDateTime>
#include <QMap>
#include <QVariant>

#define RETRY_TIMES         3
#define TIME_OUT_CODE       100
#define CRC_ERROR_CODE      101
#define MISSION_ERROR       102
#define SEND_BUFFER_SIZE    64
#define RECV_BUFFER_SIZE    64
#define TREND_RECV_SIZE     8200

enum enMissionType
{
    MT_TEMP_WRITE,
    MT_TEMP_READ,
    MT_PERIOD_WRITE,
    MT_PERIOD_READ
};

enum enMissionStatus
{
    MS_READY,
    MS_DOING,
    MS_DONE,
    MS_DELETE,
    MS_PAUSE,
    MS_RETRY,
    MS_ERROR,
    MS_BOTTOM
};

#define MISSION_TYPE        "Mission.Type"
#define MISSION_LEVEL       "Mission.Level"
#define MISSION_WRITE_DATA  "Mission.Write.Data"
#define MISSION_RETRIES     "Mission.Retries"
#define MISSION_STATUS      "Mission.Status"
#define DATA_LENGTH         "Data.Length"
#define DATA_ADDRESS        "Data.Address"
#define MISSION_PERIOD      "Mission.Period"
#define MACHINE_ADDRESS     "Machine.Address"
#define MISSION_TIME_OUT    "Mission.Timeout"
#define MISSION_UUID        "Mission.Uuid"
#define RECV_MAX_LENGTH     "Recv.Max.Length"

class COMMUNICATE_EXPORT QMission
{
public:
    QMission(enMissionType type);
    ~QMission();

    void        update_send_buffer();
    uchar*      get_send_buffer();
    uchar*      get_recv_buffer();

    int         get_send_length();
    int         get_recv_length();

    int         need_to_do();

    void        operator =(const QMission& ms);

    QVariant    get_attribute(const QString &key,const QVariant &def=QVariant());
    void        set_attribute(const QString &key,const QVariant &value);

    void        ready();
public:
    static ushort get_crc(uchar* buffer,int size);
protected:
    int         m_send_length;
    int         m_recv_length;
    uchar*      m_send_buffer;
    uchar*      m_recv_buffer;
    QDateTime   m_last_time;
    QMap<QString,QVariant>  m_attribute;

};

#endif // QMISSION_H
